package com.yxy.springcloud.framework.unionauth.controller;


import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.base.RestResponse;
import com.yxy.springcloud.framework.unionauth.DTO.ChangePasswordDto;
import com.yxy.springcloud.framework.unionauth.model.AuthAdminUser;
import com.yxy.springcloud.framework.unionauth.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * union auth系统自身登录逻辑
 */
@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {

  @Autowired
  private LoginService loginService;

  //登录
  @PostMapping("/in")
  public RestResponse in(@RequestBody AuthAdminUser loginUser) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "登陆成功");
    restResponse.setContent(loginService.in(loginUser));
    return restResponse;
  }

  @GetMapping("/out")
  public RestResponse out() throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "登出成功");
    loginService.out();
    return restResponse;
  }

  @PostMapping("/update")
  public RestResponse update(@RequestBody AuthAdminUser inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "信息修改成功");
    loginService.update(inputDto);
    return restResponse;
  }

  @GetMapping("/userInfo")
  public RestResponse userInfo(String token) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "查询成功");
    restResponse.setContent(loginService.userInfo(token));
    return restResponse;
  }

  // 修改auth管理员登录密码
  @PostMapping("/changePassword")
  public RestResponse changePassword(@RequestBody ChangePasswordDto inputDto) throws BusinessException {
    RestResponse restResponse = new RestResponse(200, "密码修改成功");
    loginService.changePassword(inputDto);
    return restResponse;
  }

}
